package org.jeecg.modules.sensor.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DeviceParam;
import org.jeecg.modules.sensor.model.EquipmentModel;
import org.jeecg.modules.sensor.model.Page;
import org.jeecg.modules.sensor.service.ISensorService;
import org.jeecg.modules.sensor.util.SensorUtils;
import org.jeecg.modules.sensor.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/sensorapi")
@Slf4j
public class SensorController {
	@Autowired
	private ISensorService sensorService;
	
	@ApiOperation("通过物联网接口获取图表数据")
	@RequestMapping(value = "/chartAnalysis", method = RequestMethod.GET)
	public Result<List<Map<Object, Object>>> chartAnalysis(
									  @RequestParam(name="deviceName", defaultValue="WHU_Sensor_G_0001") String deviceName,
									  @RequestParam(name="periodType") String periodType,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<List<Map<Object, Object>>> result = new Result<List<Map<Object, Object>>>();
		List<Map<Object, Object>> list = sensorService.chartAnalysis(deviceName, periodType, pageNo, pageSize);
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}
	
	
	@ApiOperation("通过物联网接口获取分页数据")
	@RequestMapping(value = "/queryDataByParam", method = RequestMethod.GET)
	public Result<Page<Map<Object, Object>>> queryDataByParam(
									  @RequestParam(name="deviceName", defaultValue="WHU_Sensor_G_0001") String deviceName,
									  @RequestParam(name="startTime") String startTime,
									  @RequestParam(name="endTime") String endTime,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<Page<Map<Object, Object>>> result = new Result<Page<Map<Object, Object>>>();
		Page<Map<Object, Object>> page = sensorService.queryDataByParam(startTime, endTime, deviceName, pageNo, pageSize);
		result.setSuccess(true);
		result.setResult(page);
		return result;
	}
	
	@ApiOperation("通过设备名称获取表头")
	@RequestMapping(value = "/getTableHeadTitle", method = RequestMethod.GET)
	public Result<List<Map<Object, Object>>> getTableHeadTitle(@RequestParam(name="deviceName", defaultValue="WHU_Sensor_G_0001") String deviceName) {
		Result<List<Map<Object, Object>>> result = new Result<List<Map<Object, Object>>>();
		List<Map<Object, Object>> list = new ArrayList<Map<Object,Object>>();
		String paramKey = "";
		String paramValue = "";
		String sceneType = deviceName.split("-")[0];
		if(sceneType.equals(SensorUtils.AIR_SENSOR_ATTRIBUTE)) {
			paramKey = DeviceParam.Air_Sensor_Attribute.getParamKey();
			paramValue = DeviceParam.Air_Sensor_Attribute.getParamValue();
		} else if(sceneType.equals(SensorUtils.WATER_SENSOR_ATTRIBUTE)) {
			paramKey = DeviceParam.Water_Sensor_Attribute.getParamKey();
			paramValue = DeviceParam.Water_Sensor_Attribute.getParamValue();
		} else if(sceneType.equals(SensorUtils.SOIL_SENSOR_ATTRIBUTE)) {
			paramKey = DeviceParam.Soil_Sensor_Attribute.getParamKey();
			paramValue = DeviceParam.Soil_Sensor_Attribute.getParamValue();
		} else if(sceneType.equals(SensorUtils.GAS_SENSOR_ATTRIBUTE)) {
			paramKey = DeviceParam.Gas_Sensor_Attribute.getParamKey();
			paramValue = DeviceParam.Gas_Sensor_Attribute.getParamValue();
		}
		for(int i = 0; i < paramKey.split(",").length; i++) {
			Map<Object, Object> map = new HashMap<Object, Object>();
			map.put("title", paramValue.split(",")[i]);
			map.put("align", "center");
			map.put("dataIndex", paramKey.split(",")[i]);
			list.add(map);
		}
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}
	
	@GetMapping("/gisPage")
    public ModelAndView gisPage() {
		String token = "br7njz5c"; //页面上的token
        String midToken = TokenUtil.encodeBCrypt(token);//生成中间token
        String hashToken = TokenUtil.decodeBase64(midToken); //生成hashToken
        String URL = "https://gis.aliyun-iot-share.com/auth/gis/" + hashToken + "/866049dd0f03471081451189c0bdc570";
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:"+URL);
        return mv;
    }
	
	@GetMapping("/gisWPage")
    public ModelAndView gisWPage() {
		String token = "DYbtfuFg"; //页面上的token
        String midToken = TokenUtil.encodeBCrypt(token);//生成中间token
        String hashToken = TokenUtil.decodeBase64(midToken); //生成hashToken
        String URL = "https://gis.aliyun-iot-share.com/auth/gis/" + hashToken + "/dcb44bfb6ff64b779aa00fa75f485a7b";
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:"+URL);
        return mv;
    }
	
	@GetMapping("/gisGPage")
    public ModelAndView gisGPage() {
		String token = "7rjhRVvB"; //页面上的token
        String midToken = TokenUtil.encodeBCrypt(token);//生成中间token
        String hashToken = TokenUtil.decodeBase64(midToken); //生成hashToken
        String URL = "https://gis.aliyun-iot-share.com/auth/gis/" + hashToken + "/bdfbde5efbb346adac8dd0d5be3cd0c9";
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:"+URL);
        return mv;
    }
	
	/**
	 * 分享场景
	 * 
	 * @return
	 */
	@GetMapping("/shareScene")
    public ModelAndView shareScene(String sceneType) {
		String token = "POEEqrkn"; //页面上的token
		String url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/fca48182e703486a9b48e1133943e25a";
		if(sceneType.equals(SensorUtils.ALL_SENSOR_ATTRIBUTE)) {
			token = "POEEqrkn";
			url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/fca48182e703486a9b48e1133943e25a";
		} else if(sceneType.equals(SensorUtils.AIR_SENSOR_ATTRIBUTE)) {
			token = "7rjhRVvB";
			url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/bdfbde5efbb346adac8dd0d5be3cd0c9";
		} else if(sceneType.equals(SensorUtils.WATER_SENSOR_ATTRIBUTE)) {
			token = "DYbtfuFg";
			url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/dcb44bfb6ff64b779aa00fa75f485a7b";
		} else if(sceneType.equals(SensorUtils.SOIL_SENSOR_ATTRIBUTE)) {
			token = "gXB2QjHv";
			url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/3c841aacf80d4b2cbd70d906991ebb9e";
		} else if(sceneType.equals(SensorUtils.GAS_SENSOR_ATTRIBUTE)) {
			token = "JvO8WYI2";
			url = "https://gis.aliyun-iot-share.com/auth/gis/{hashToken}/c3ab88ced7294f19a06a47e04c1ded13";
		}
        String midToken = TokenUtil.encodeBCrypt(token);//生成中间token
        String hashToken = TokenUtil.decodeBase64(midToken); //生成hashToken
        String URL = url.replace("{hashToken}", hashToken);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:"+URL);
        return mv;
    }
	
}
